| Using the Registry with Visual Basic |
| Written:
7/23/2001 7:00 AM By: venky_dude |
| This Article will tell you about the registry and how to use it for saving/reading data from it using visual basic. |
|
Using the registry with Visual Basic-I (Creating keys and storing values)
The registry is a very important part of the Windows operating system. The registry can be thought of as a place where windows and other application keep important information which is needed by them. This information could be about specific hardware or software configuration. The registry gives us immense power to store and retrieve data from it. But with power comes responsibility. A small error in the registry could bring the windows OS crashing down. Before we get into programming/using the registry let's take a brief look at the structure of the registry. . The registry structure is similar to a tree structure. The 6 main branches in the registry are called "HIVES". These branches or "HIVES" are as follows
The hives are made up of KEYS. These keys can have SUBKEYS as well as Values. The values contain the actual information in the registry. Looking at the various HIVES we can come to a conclusion that the HIVES which are apt for creating KEYS and storing values for our programs are HKEY_CURRENT_USER & HKEY_LOCAL_MACHINE. Let us now look at creating keys and storing values. The Api's which we shall use are as follows Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long Private Declare Function RegSetValueEx Lib "advapi32.dll"
Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal
Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As
Long Private Declare Function RegQueryValueEx Lib "advapi32.dll"
Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String,
ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As
Long and also the following constants Private Const HKEY_CURRENT_USER = &H80000001 Private Const HKEY_LOCAL_MACHINE = &H80000002 Now let's create a key in HKEY_LOCAL_MACHINE.The code we shall use for doing this is as follows Dim result As Long The first parameter in the RegCreateKey function is the main KEY or HIVE under which we want to create a subkey to input values. In this particular case the parameter is the hive HKEY_LOCAL_MACHINE. The second parameter is the subkey which we want to create. In this case we want to create a subkey RegDemo under 'Software' Key. The third parameter will ' contain the 'handle', a unique identifier for the key which was created .This parameter is filled with the value of the handle if the function was executed succesfully. Here we have created a new Key called RegDemo which is the name of the sample application under "HKEY_LOCAL_MACHINE\Software". Now we can put in all the data under the RegDemo Key. Now we shall put in some values under this key. This we shall do by using RegSetValue. Let us assume we have this application where we have to save the name of the person who last used it ,date when the application was last used and the time when it was used. Before we can set a value in a key we need to open that key using RegOpenKey.
Dim result As Long The parameter in RegOpenKey are similar to the parameter of RegCreateKey After executing this statement we will get a 'handle' to the key which we have opened.This handle is stored in keyres. After we have opened this key let us put in a name as a value Dim entry As String The first parameter in RegSetValueEx function is the handle to
the key in which values have to be stored. We get this handle by using the
RegOpenKey function.The second parameter is the value name in this case it is
"Name",the Here txtName.text is a value entered by the user. Let us also put in the current date Dim entry As String and the current time Dim entry As String
In order to read values from the registry we need to use the RegQueryValueEx Api function.If we have to read the values that we have put in the registry we do it in the following way.We first open the key whose value is to be read. Dim result As Long After opening the key we get it's handle in keyres. Now we use the RegQueryValueEx function in the following way entry = "Name" The first parameter in the RegQueryValueEx function is the handle to the key to be opened for reading ,in this case keyres,2nd parameter is the value name to be read here it is "Name". The 3rd parameter is reserved and should be 0.The 4th parameter is also 0.The 5th parameter is a pointer to a string variable which will hold the value of the key after the function is succesfully executed. The 6th parameter is the length of the value.Notice that we have used the RegQueryValueEx function twice. The first time we use it to get the length of the value .After We get the length ,we fill the string variable with empty space in order to hols the result.After that we call RegQueryValueEx again and this time after successfull execution val1 will conatin the value of the key. Similarly we can get the values of "Date" entry = "Date" & "Time" entry = "Time"
In this article we have learnt how to create registry entries and read/write values in them .If you have any questions/comments do send them to venky@venkydude.com .You can check out some cool codes made by me in VB from my website here
|